package org.torproject.android;

import java.util.List;

import java.util.Timer;
import java.util.TimerTask;

import org.torproject.android.service.ITorService;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Debug;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CircuitStatusListView extends ListActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Debug.startMethodTracing("CircuitStatusListView");
		
		setContentView(R.layout.layout_circuitstatus);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		
		mConnection = new ITorServiceConnection();
		bindService();

    	timerTask = new TimerTask() {

			@Override
			public void run() {
				new GetInfoTask().execute("circuit-status");
			}
    	};		
    	
    	timer = new Timer(true);
		getInfoTask = new GetInfoTask();
		adapter = new ArrayAdapter<Object>(CircuitStatusListView.this, R.layout.layout_circuit_row);
		setListAdapter(adapter);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		Debug.stopMethodTracing();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		timer.cancel();
	}
	
	@Override
	protected void onListItemClick(ListView  l, View  v, int position, long id) {
		Intent intent = new Intent(this, CircuitStatus.class);
		intent.putExtra("org.torproject.android.CircuitToClose", openCircuits.get(position));
		startActivity(intent);
	}
	
    private void bindService ()
    {
    	 bindService(new Intent(ITorService.class.getName()),
                 mConnection, Context.BIND_AUTO_CREATE);    	 
    }
        
    private class ITorServiceConnection implements ServiceConnection {

    	public void onServiceConnected(ComponentName className, IBinder service) {

        	mService = ITorService.Stub.asInterface(service);
        	timer.schedule(timerTask, 0, 10000);
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            mService = null;
        }
    };

    private class GetInfoTask extends AsyncTask<String, Integer, List<Circuit>> {
        
    	@Override
    	protected List<Circuit> doInBackground(String... urls) {
            try {
				return mService.getCurrentCircuits();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			return null;
        }

    	@Override
        protected void onPostExecute(List<Circuit> result) {
    		openCircuits = result;
        	adapter.clear();
			for(Circuit circuit : result) {
				adapter.add(circuit);
			}
			adapter.notifyDataSetChanged();
        }
    }
    
    ITorService mService;
    Timer timer;
    GetInfoTask getInfoTask;
    TimerTask timerTask;
    ArrayAdapter<Object> adapter;
    List<Circuit> openCircuits;
    ServiceConnection mConnection;    
}
